package br.com.android.pubclub.negocios;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import br.com.android.pubclub.R;
import br.com.android.pubclub.basicas.Estabelecimento;
import br.com.android.pubclub.dados.EstabelecimentoAdapter;
import br.com.android.pubclub.dados.WsServicos;
import br.com.android.pubclub.web.RotaMapaActivity;

public class ListagemActivity extends Activity implements OnItemClickListener{
	
	ProgressDialog dialog;
	private ListView listView;
	ArrayList<Estabelecimento> estabelecimentos;
	EstabelecimentoAssyncTask task;
	private final int  LOCALIZACAO_ATUAL = 1;
	String operacao;
	private String latitude;
	private String longitude;
	private Estabelecimento estabelecimento;
	@Override
	protected void onCreate(Bundle savedInstanceState) { 
		super.onCreate(savedInstanceState);
		
		dialog = ProgressDialog.show(this, "", "Carregando...", true);
		
		try {
			operacao = getIntent().getStringExtra("operacao");
			setContentView(R.layout.activity_listagem);
			estabelecimentos = new ArrayList<Estabelecimento>();
			iniciarDownload();
			listView = (ListView)findViewById(R.id.ListView1);
			
			ArrayAdapter<Estabelecimento> adapter = 
					new ArrayAdapter<Estabelecimento>(
							this, 
							android.R.layout.simple_list_item_1,
							estabelecimentos);
			
			listView.setAdapter(adapter);
			listView.setOnItemClickListener(this);
		} catch (Exception vE) {
			System.out.println(vE.getMessage());
		}
		
	}
	
	private void iniciarDownload() {
		
		ConnectivityManager cm = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (cm.getActiveNetworkInfo() != null && cm.getActiveNetworkInfo().isConnected()) {

			task = new EstabelecimentoAssyncTask();
			task.execute();

		} else {
			Toast.makeText(this, "Sem conex‹o com a Internet", Toast.LENGTH_SHORT).show();
		}
	}

	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.listagem, menu);
		return true;
	}
	
	
	@Override
	public void onItemClick(AdapterView<?> pArg0, View pArg1, int posicao, long pArg3) {
			
		MapaActivity cdf = MapaActivity.novaInstancia(estabelecimentos.get(posicao));
		if((operacao!=null)&&(operacao.equals("1"))){
			Intent it = new Intent(this, RotaMapaActivity.class);
			it.putExtra("latitude",estabelecimentos.get(posicao).latitude);
			it.putExtra("longitude",estabelecimentos.get(posicao).longitude);
			startActivity(it);
		}else{
			Intent it = new Intent(this, MapaActivity.class);
			it.putExtra("estabelecimento",estabelecimentos.get(posicao));
			startActivity(it);
		}
	}
	
	public void refreshList() {
		EstabelecimentoAdapter  adapter = new EstabelecimentoAdapter(this, estabelecimentos);
		listView.setAdapter(adapter);
	}
	class EstabelecimentoAssyncTask extends AsyncTask<Void, Void, ArrayList<Estabelecimento>>{
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
		}
		@Override
		protected ArrayList<Estabelecimento> doInBackground(Void... params){
			try {

				Intent it = new Intent(ListagemActivity.this, WsServicos.class);
				it.putExtra("parametro", "listaEst");
				startActivity(it);

				return WsServicos.retrieveListaEstabelecimento();
			} catch (Exception e) {
				e.printStackTrace();
			}finally{
				dialog.dismiss();
			}
			return null;
		}
		@Override
		protected void onPostExecute(ArrayList<Estabelecimento> lista) {
			super.onPostExecute(lista);			
			if(lista!=null){
				estabelecimentos = lista;
				refreshList();
				
			}
		}

	}
	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==1){
			if(resultCode==RESULT_OK){
				latitude = (String)data.getStringExtra("latidude");
				longitude = (String)data.getStringExtra("longitude");
				Intent itent = new Intent(this, PubClubRotaMapaActivity.class);							
				itent.putExtra("estabelecimento",estabelecimento);
				itent.putExtra("latitude", latitude);
				itent.putExtra("longitude", longitude);
				startActivity(itent);
			}
		
		}
	}
}
